extern crate fejpsl_contract;

use self::fejpsl_contract::community::table::{TablePageData, Table, TableListData};

pub async fn get_room_table_list_by_page_no(room_id:&i32,page_no:&i32) ->TablePageData {
    let client = super::super::db::connect().await.unwrap();
    let mut vec_table = Vec::new();
    let per_page_num:i64=10;
    let offset:i64;
    offset=*page_no as i64 *per_page_num;
    let rows = &client.query("SELECT id,room_id,table_no,table_desc,is_encrypted, table_password
		 from t_room_table where room_id=$3 limit $1 offset $2",
                             &[&per_page_num,&offset,&room_id]).await.unwrap();
    for row in rows {
        let table = Table {
            id:                 row.get(0),
            room_id:          row.get(1),
            table_no:    row.get(2),
            table_desc:        row.get(3),
            is_encrypted:            row.get(4),
            table_password:           row.get(5),
        };

        vec_table.push(table);
    }

    let total=rows.len() as i32;
    let page_count=(total/10) as i32;
    let mut over=false;
    if page_no>=&page_count {over=true}
    let size=per_page_num as i32;
    let table_list_data= TableListData {
        cur_page:*page_no,
        datas:Some(vec_table),
        offset: offset,
        over: over,
        page_count:page_count,
        size:size,
        total:total,
    };
    let table_page_data= TablePageData {
        data:table_list_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    return table_page_data;

}
